const mongoose = require('mongoose')
// const mongodbUrl = 'mongodb+srv://ryu:vyKXPFxAlvUUwYHY@cluster0-wviyl.mongodb.net/ftc?retryWrites=true&w=majority' //线上
const mongodbUrl = 'mongodb://localhost:27017/ftc' // 本地
mongoose.set('useFindAndModify', false)
mongoose.connect(mongodbUrl,
  {
    useNewUrlParser: true ,
    useUnifiedTopology: true,
  }, 
 function (err) {
  if (err) {
    console.log(err)
  } else {
    console.log('数据库连接成功,可以搞事情!')
  }
})
const Schema = mongoose.Schema

// 验证码
let checkcodeSchema = new Schema({
  token: String,
  code: String
})
// 用户
let userSchema = new Schema({
  user_name: String,
  user_id: String,
  user_pwd: String,
  company: String,
  avatar: {
    type: String,
    default: ''
  },
  token: {
    type: String,
    default: ''
  }
})
// 访问人数
let visitorSchema = new Schema({
  count: Number
})
// 发送的TUI
let commitSchema = new Schema({
  timestamp: {
    type: String,
    default: Date.now,
  },
  content: String, // 内容
  tags: String, // 标签
  like: 0, //点赞
  comment:0,
  images: Array, //图片
  parseUrl:{
    title: {
      type: String,
      default: '',
    },// 标题
    url: {
      type: String,
      default: '',
    }, // 地址
    thumb: {
      type: String,
      default: '',
    }, // 图标
  },
  user_id: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
  },
  user_name: {
    type: String,
    ref: 'User',
  },
  user_avatar: {
    type: String,
    ref: 'User',
  },
  user_company: {
    type: String,
    ref: 'User',
  }
})

let tagSchema = new Schema({
  name: String,
})

let commentSchema = new Schema({
  timestamp: {
    type: String,
    default: Date.now,
  },
  compose_id: {
    type: mongoose.Schema.ObjectId,
    ref: 'Commit'
  },// 作品的ID 
  content: String, // 评论内容
  from_user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User'
  }, //谁发起的评论
  to_user:{
    type: mongoose.Schema.ObjectId,
    ref: 'User'
  },
  reply:{
    type:mongoose.Schema.ObjectId,
    ref: 'Reply'
  },
  like:Number,
})

let replySchema = new Schema({
  comment_id:{
    type: mongoose.Schema.ObjectId,
    ref: 'Comment'
  }, // 对应的某条留言
  content:String, // 回复内容
  from_user_id:{
    type: mongoose.Schema.ObjectId,
    ref: 'User'
  }, // 谁发起的回复
  to_user_id:{
    type: mongoose.Schema.ObjectId,
    ref: 'User'
  } // 收的回复
})

let zanSchema = new Schema({
  type_id:{
    type: mongoose.Schema.ObjectId,
    ref: 'Commit'
  },// 作品或评论得id
  type:Number,// 作品或者评论
  user_id:{
    type: mongoose.Schema.ObjectId,
    ref: 'User'
  }, // 点赞用户
  status:Number, // 0 未点赞 1 点赞
})
let phoneSchema = new Schema({
  host: String,
  userAgent: String,
  ua: String,
})

let shadiaoSchema = new Schema({
  words: String,
  createtime: {
    type: String,
    default: Date.now,
  }
})

exports.CheckCode = mongoose.model('Checkcode', checkcodeSchema)
exports.Visitor = mongoose.model('Visitor', visitorSchema)
exports.Commit = mongoose.model('Commit', commitSchema)
exports.User = mongoose.model('User', userSchema)
// exports.Like = mongoose.model('Like', likedSchema)
exports.Tag = mongoose.model('Tag', tagSchema)
exports.Comment = mongoose.model('Comment', commentSchema)
exports.Reply = mongoose.model('Reply', replySchema)
exports.Zan = mongoose.model('Zan', zanSchema)
exports.Phone = mongoose.model('Phone', phoneSchema)
exports.Words = mongoose.model('Words', shadiaoSchema)